﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Linq;

namespace XEvents.Library.Targets
{
    public class Target_Counter : TargetData
    {
        private Target_Counter(SqlServer server, EventSessionTarget parent)
            : base(server, parent)
        { }
        protected override void LoadData()
        { }

        protected override void InitalizeTarget()
        {
            //throw new NotImplementedException();
        }

        /*
        <CounterTarget truncated="0">
          <Packages>
            <Package name="package0" />
            <Package name="sqlos" />
            <Package name="sqlserver">
              <Event name="sql_statement_starting" count="692" />
              <Event name="sql_statement_completed" count="691" />
            </Package>
            <Package name="SecAudit" />
          </Packages>
        </CounterTarget>
        */

        public System.Data.DataTable GetEventsAsDataTable()
        {
            System.Data.DataTable dt = new System.Data.DataTable("EventInformation");

            dt.Columns.Add("Package");
            dt.Columns.Add("Event");
            dt.Columns.Add("Count");

            
            foreach (CountedPackage p in Packages)
            {
                foreach (CountedEvent e in p.Events)
                {
                    System.Data.DataRow dr = dt.Rows.Add();
                    dr["Package"] = p.Name;
                    dr["Event"] = e.Name;
                    dr["Count"] = e.Count;
                }
            }
            return dt;
        }

        internal static TargetData GetTargetData(string data, SqlServer server, EventSessionTarget parent)
        {
            XDocument targetdata = XDocument.Parse(data);

            return (from e in targetdata.Elements()
                    select new Target_Counter(server, parent)
                    {
                        truncated = (int)e.Attribute("truncated"),
                        countedpackage = new List<CountedPackage>(
                            from d in e.Elements()
                            from cp in d.Elements()
                            select new CountedPackage
                            {
                                name = (string)cp.Attribute("name"),
                                countedevents = new List<CountedEvent>(
                                    from ce in cp.Elements()
                                    select new CountedEvent
                                    {
                                        name = (string)ce.Attribute("name"),
                                        count = (int)ce.Attribute("count")
                                    })
                            })
                    }).ElementAt(0);
        }

        private int truncated;
        private List<CountedPackage> countedpackage;

        public List<CountedPackage> Packages
        {
            get { return countedpackage; }
        }
        public int Truncated
        {
            get { return truncated; }
        }
    }
}
